package cn.rongdao.jrkabao.ui.cashcoupon.mycashcoupon.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.mifengkong.frtools.http.request.callback.FRBaseObserver;
import com.mifengkong.frwidget.recyclerview.PtrRecyclerViewUIComponent;
import com.mifengkong.frwidget.refersh.OnPullToRefreshListener;

import java.util.List;

import butterknife.BindView;
import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.base.BaseViewPagerFragment;
import cn.rongdao.jrkabao.bean.cashcoupon.GetCashCouponBean;
import cn.rongdao.jrkabao.model.cashcoupon.GetCashCouponModel;
import cn.rongdao.jrkabao.ui.cashadvance.uicomponent.CashEmptyUIComponent;
import cn.rongdao.jrkabao.ui.cashcoupon.mycashcoupon.adapter.CashCouponAdapter;

/**
 * Created by jack on 2017/8/14
 */

public abstract class BaseFragmentCashCoupon extends BaseViewPagerFragment {

    @BindView(R.id.fbcc_ptrframelayout)
    PtrRecyclerViewUIComponent ptrRecyclerViewUIComponent;
    @BindView(R.id.fbcc_empty_view)
    CashEmptyUIComponent cashEmptyUIComponent;

    protected CashCouponAdapter mAdapter;

    @Override
    protected int layoutRes() {
        return R.layout.fragment_base_cash_coupon;
    }

    @Override
    protected void onViewReallyCreated(View view) {
        super.onViewReallyCreated(view);
        mAdapter = new CashCouponAdapter(getActivity());

        ptrRecyclerViewUIComponent.setBackground(R.color.layout_bg);
        ptrRecyclerViewUIComponent.setLayoutManager(new LinearLayoutManager(getActivity()));
        ptrRecyclerViewUIComponent.setAdapter(mAdapter);

        initView();

        ptrRecyclerViewUIComponent.setOnPullToRefreshListener(new OnPullToRefreshListener() {
            @Override
            public void onPullToRefresh() {
                requestData(getIndex());
            }
        });
    }

    @Override
    protected void lazyLoad() {
        super.lazyLoad();
        ptrRecyclerViewUIComponent.delayRefresh();
    }

    protected abstract int getIndex();

    protected void initView() {
    }

    private void requestData(final int index) {
        GetCashCouponModel.getInstance().execute(index, new FRBaseObserver<List<GetCashCouponBean>>(this) {
            @Override
            protected void onBaseNext(List<GetCashCouponBean> data) {
                if (null != data && data.size() > 0) {
                    mAdapter.setDataList(data);
                    cashEmptyUIComponent.setVisibility(View.GONE);
                    ptrRecyclerViewUIComponent.setVisibility(View.VISIBLE);
                } else {
                    cashEmptyUIComponent.initCashCouponEmpty(index);
                    ptrRecyclerViewUIComponent.setVisibility(View.GONE);
                }
                ptrRecyclerViewUIComponent.refreshComplete();
            }

            @Override
            protected void onBaseError(Throwable t) {
                super.onBaseError(t);
                cashEmptyUIComponent.initCashCouponEmpty(index);
                ptrRecyclerViewUIComponent.setVisibility(View.GONE);
                ptrRecyclerViewUIComponent.refreshComplete();
            }
        });
    }
}
